@inherits AppComponentBase

@if (isOtpSent || requiresTwoFactor)
{
    <!-- Check out ShowSignInPanel's comments -->
    <NavigationLock OnBeforeInternalNavigation="HideTfaAndOtpPanels" />
}

<section>
    <BitStack HorizontalAlign="BitAlignment.Center" FillContent>
        <EditForm Model="model" OnSubmit="WrapHandled(DoSignIn)" novalidate>
            <AppDataAnnotationsValidator @ref="validatorRef" />

            <BitStack HorizontalAlign="BitAlignment.Center">
                @if (requiresTwoFactor is false)
                {
                    @if (isOtpSent is false)
                    {
                        <BitStack HorizontalAlign="BitAlignment.Center" FillContent>
                            <BitStack>
                                <BitText Typography="BitTypography.H4">@Localizer[nameof(AppStrings.SignInPanelTitle)]</BitText>

                                <BitText Typography="BitTypography.Subtitle1" Color="BitColor.SecondaryForeground">
                                    @Localizer[nameof(AppStrings.SignInPanelSubtitle)]
                                </BitText>
                            </BitStack>

                            <SocialRow IsWaiting="isWaiting" OnClick="SocialSignIn" />

                            <BitSeparator Border="BitColorKind.Tertiary" Background="BitColorKind.Secondary" Class="lg-sep">@Localizer[AppStrings.Or]</BitSeparator>
                            <BitSeparator Border="BitColorKind.Secondary" Background="BitColorKind.Primary" Class="sm-sep">@Localizer[AppStrings.Or]</BitSeparator>

                            <BitStack FillContent Gap="2rem">
                                <BitStack FillContent>
                                    <BitPivot Alignment="BitAlignment.Center" HeaderOnly OnChange="OnTabChange">
                                        <BitPivotItem HeaderText="@Localizer[nameof(AppStrings.Email)]" Key="@SignInPanelTab.Email.ToString()" />
                                        <BitPivotItem HeaderText="@Localizer[nameof(AppStrings.Phone)]" Key="@SignInPanelTab.Phone.ToString()" />
                                    </BitPivot>

                                    <BitStack Gap="0" FillContent>
                                        @if (currentTab is SignInPanelTab.Email)
                                        {
                                            <BitTextField @bind-Value="model.Email"
                                                          TabIndex="1"
                                                          Type="BitInputType.Email"
                                                          Immediate DebounceTime="500"
                                                          Label="@Localizer[nameof(AppStrings.Email)]"
                                                          Placeholder="@Localizer[nameof(AppStrings.EmailPlaceholder)]" />
                                            <ValidationMessage For="@(() => model.Email)" />
                                        }
                                        else
                                        {
                                            <BitTextField @bind-Value="model.PhoneNumber"
                                                          AutoFocus TabIndex="1"
                                                          Type="BitInputType.Tel"
                                                          Immediate DebounceTime="500"
                                                          Label="@Localizer[nameof(AppStrings.PhoneNumber)]"
                                                          Placeholder="@Localizer[nameof(AppStrings.PhoneNumberPlaceholder)]" />
                                            <ValidationMessage For="@(() => model.PhoneNumber)" />
                                        }
                                        @if (internalSignInPanelType is SignInPanelType.Full or SignInPanelType.Password)
                                        {
                                            <br />
                                            <BitTextField @bind-Value="model.Password"
                                                          TabIndex="2"
                                                          CanRevealPassword="true"
                                                          Type="BitInputType.Password"
                                                          AutoComplete="@BitAutoCompleteValue.CurrentPassword"
                                                          Placeholder="@Localizer[nameof(AppStrings.PasswordPlaceholder)]">
                                                <LabelTemplate>
                                                    <BitStack Horizontal VerticalAlign="BitAlignment.Center">
                                                        <BitText>@Localizer[nameof(AppStrings.Password)]</BitText>
                                                        <BitSpacer />
                                                        <BitLink Href="@($"{PageUrls.ForgotPassword}?return-url={Uri.EscapeDataString(GetReturnUrl())}")">@Localizer[nameof(AppStrings.ForgotPasswordLink)]</BitLink>
                                                    </BitStack>
                                                </LabelTemplate>
                                            </BitTextField>
                                            <ValidationMessage For="@(() => model.Password)" />
                                        }
                                    </BitStack>
                                </BitStack>

                                <BitStack Horizontal Alignment="BitAlignment.Center">
                                    <BitCheckbox @bind-Value="model.RememberMe" Label="@Localizer[nameof(AppStrings.RememberMe)]" />
                                    <BitSpacer />
                                    @if (showWebAuthn)
                                    {
                                        <BitButton IconOnly
                                                   Size="BitSize.Large"
                                                   IsLoading="isWaiting"
                                                   Variant="BitVariant.Text"
                                                   Color="BitColor.Tertiary"
                                                   IsEnabled="isWaiting is false"
                                                   ButtonType="BitButtonType.Button"
                                                   IconName="@BitIconName.Fingerprint"
                                                   OnClick="WrapHandled(PasswordlessSignIn)" />
                                    }
                                </BitStack>

                                @if (internalSignInPanelType is SignInPanelType.Full or SignInPanelType.Password)
                                {
                                    <BitStack Alignment="BitAlignment.Center" FitHeight FillContent>
                                        @if (internalSignInPanelType is SignInPanelType.Password)
                                        {
                                            <BitText Typography="BitTypography.Body2" Align="BitTextAlign.Center">
                                                <b>@Localizer[nameof(AppStrings.SignInPasswordMessage1)]</b>
                                                <br />
                                                @Localizer[nameof(AppStrings.SignInPasswordMessage2)]
                                            </BitText>
                                        }
                                        <BitButton IsLoading="isWaiting"
                                                   IsEnabled="isWaiting is false"
                                                   ButtonType="BitButtonType.Submit">
                                            @Localizer[nameof(AppStrings.Continue)]
                                        </BitButton>
                                    </BitStack>
                                }

                                @if (internalSignInPanelType is SignInPanelType.Full or SignInPanelType.Otp)
                                {
                                    <BitButton IsLoading="isWaiting"
                                               ButtonType="BitButtonType.Button"
                                               OnClick="WrapHandled(() => SendOtp(resend: true))"
                                               Variant="@(internalSignInPanelType is SignInPanelType.Otp ? BitVariant.Fill : BitVariant.Outline)"
                                               IsEnabled="@(isWaiting is false && (model.Email is not null || model.PhoneNumber is not null))">
                                        @(currentTab == SignInPanelTab.Email ? Localizer[nameof(AppStrings.SendMagicLinkButtonText)] : Localizer[nameof(AppStrings.SendOtpButtonText)])
                                    </BitButton>
                                }

                                @if (internalSignInPanelType is SignInPanelType.Otp)
                                {
                                    <BitLink OnClick="() => ChangeSignInPanelType(SignInPanelType.Password)">@Localizer[nameof(AppStrings.SignInByPassword)]</BitLink>
                                }

                                @if (internalSignInPanelType is SignInPanelType.Password)
                                {
                                    <BitLink OnClick="() => ChangeSignInPanelType(SignInPanelType.Otp)">@Localizer[nameof(AppStrings.SignInByOtp)]</BitLink>
                                }

                                @if (internalSignInPanelType is SignInPanelType.Full)
                                {
                                    <BitText Align="BitTextAlign.Center" Typography="BitTypography.Body2">
                                        @Localizer[nameof(AppStrings.DontHaveAccountMessage)]
                                        <BitLink Href="@($"{PageUrls.SignUp}?return-url={Uri.EscapeDataString(GetReturnUrl())}")">@Localizer[nameof(AppStrings.SignUp)]</BitLink>
                                    </BitText>
                                }
                            </BitStack>
                        </BitStack>
                    }
                    else
                    {
                        <OtpPanel Model="model"
                                  OnSignIn="DoSignIn"
                                  IsWaiting="isWaiting"
                                  OnResendOtp="WrapHandled(() => SendOtp(resend:true))" />
                    }
                }
                else
                {
                    <TfaPanel model="model"
                              IsWaiting="isWaiting"
                              OnTokenProvided="DoSignIn"
                              OnSendTfaToken="WrapHandled(SendTfaToken)" />
                }
            </BitStack>
        </EditForm>
    </BitStack>
</section>
